<template>
  <div>
    <div class="header">
      <span style="margin-left: 20px">关键字</span>
      <el-input
        placeholder="请输入关键字"
        v-model="req.keyword"
        style="width: 200px; margin-left: 10px"
        clearable
      ></el-input>
      <el-button
        type="primary"
        style="margin-left: 20px"
        size="small"
        @click="
          req.pageIndex = 1
          getPrivacyPolicies()
        "
      >
        搜索
      </el-button>
      <el-button
        type="text"
        style="margin-left: 20px"
        size="small"
        @click="toAddPage"
      >
        新增协议
      </el-button>
    </div>
    <el-table
      style="overflow: auto"
      :data="res.Items"
      stripe
      border
      highlight-current-row
    >
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column label="标题" prop="PPC_App"></el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            icon="el-icon-edit"
            @click="toEditPage(scope.row.PPC_App)"
          >
            编辑
          </el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-delete"
            @click="deletePrivacyPolicy(scope.row.PPC_ID)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="width: 100%; height: 40px">
      <el-pagination
        layout="total,prev,pager,next,sizes"
        style="float: right"
        background
        :current-page.sync="req.pageIndex"
        :total="res.Total"
        :page-size="req.pageSize"
        :page-sizes="[10, 15, 20]"
        @size-change="
          (val) => {
            req.pageSize = val
            getPrivacyPolicies()
          }
        "
        @current-change="getPrivacyPolicies()"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import {
  getPrivacyPolicies,
  deletePrivacyPolicy,
} from '@/api/common/privacyPolicy'
export default {
  data: function () {
    return {
      keyword: '',
      req: {
        pageIndex: 1,
        pageSize: 10,
        keyword: null,
      },
      res: {
        Items: [],
        Total: 0,
      },
    }
  },
  methods: {
    toAddPage() {
      this.$router.push({
        name: 'addPrivacyPolicy',
      })
    },
    toEditPage(app) {
      this.$router.push({
        name: 'editPrivacyPolicy',
        query: { app },
      })
    },
    getPrivacyPolicies() {
      getPrivacyPolicies(this.req).then(({ data }) => (this.res = data))
    },
    deletePrivacyPolicy(id) {
      this.$confirm(`是否删除该协议`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deletePrivacyPolicy(id).then(() => {
          this.getPrivacyPolicies()
        })
      })
    },
  },
  created: function () {
    var data = this.$store.getters['pagination/getRoutePage'](this.$route)
    Object.assign(this.$data, data)
    this.getPrivacyPolicies()
  },
  beforeRouteLeave(to, from, next) {
    this.$store.dispatch('pagination/setRoutePage', {
      route: this.$route,
      pageData: this.$data,
    })
    next()
  },
}
</script>
<style>
.el-drawer.rtl {
  overflow: scroll;
}
</style>
<style scoped>
.header {
  width: 100%;
  padding-top: 10px;
  padding-bottom: 10px;
  height: auto;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  background: #ffffff;
  border-radius: 5px;
  border-bottom: #f4f8ff 2px solid;
  display: flex;
  margin-bottom: 11px;
  align-items: center;
}
</style>
